package com.zjk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjk.PageResult;
import com.zjk.category.CategoryBrandRelationEntity;
import com.zjk.dto.BrandDTO;
import com.zjk.mapper.BrandMapper;
import com.zjk.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: gfk
 * @create: 2022/10/13
 * @Description:
 * @FileName: BrandService
 * @History:
 * @自定义内容：
 */
@Service
public class BrandService {
    @Autowired(required = false)
    BrandMapper brandMapper;

    @Autowired
    CategoryBrandRelationService relationService;

    /**
     * 分页查询
     * @param searchMap
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult list(Map searchMap, Integer pageNum, Integer pageSize) {
        //返回的数量 total 当前页的记录集
        Page<Brand> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();

        String keyword = (String) searchMap.get("keyword");
        if (!StringUtils.isEmpty(keyword)){
            wrapper.lambda().like(Brand::getName,keyword)
                    .or().like(Brand::getFirstLetter,keyword)
                    .or().eq(Brand::getDescript,keyword);
        }
        Page<Brand> brandPage = brandMapper.selectPage(page, wrapper);



        long total = brandPage.getTotal();
        List<Brand> records = brandPage.getRecords();
        List<BrandDTO> collect = records.stream().map((brandEntity) -> {
            BrandDTO dto = new BrandDTO();
            BeanUtils.copyProperties(brandEntity, dto);

            List<CategoryBrandRelationEntity> categoryBrandRelationList =
                    relationService.findCategoryBrandRelationByBrandId(brandEntity.getBrandId());
            dto.setCategoryBrandRelationList(categoryBrandRelationList);
            return dto;
        }).collect(Collectors.toList());

        return new PageResult(total,collect);
    }

    /**
     * 查询所有
     * @return
     */
    public List<Brand> lists() {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        List list = brandMapper.selectList(wrapper);
        return list;
    }


    public void add(Brand brand) {
        brandMapper.insert(brand);

    }

    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }
     public void delete(Long brandId) {
        brandMapper.deleteById(brandId);
    }

    public PageResult<BrandDTO> selectBrandDTOList(Integer pageNo, Integer pageSize, Map searchMap) {
        //返回的数量 total 当前页的记录集
        Page<Brand> page =new Page<>(pageNo,pageSize);

        QueryWrapper<Brand> queryWrapper = new QueryWrapper();
        String keyword = (String)searchMap.get("keyword");
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.lambda().
                    like(Brand::getName,keyword)
                    .or()
                    .like(Brand::getDescript,keyword)
                    .or()
                    .eq(Brand::getFirstLetter,keyword);

        }
        Page<Brand> brandPage = brandMapper.selectPage(page, queryWrapper);
        //where        limit
        long total = brandPage.getTotal();
        List<Brand> records = brandPage.getRecords();
        List<BrandDTO> collect = records.stream().map((brandEntity) -> {
            BrandDTO dto = new BrandDTO();
            BeanUtils.copyProperties(brandEntity, dto);

            List<CategoryBrandRelationEntity> categoryBrandRelationList =
                    relationService.findCategoryBrandRelationByBrandId(brandEntity.getBrandId());
            dto.setCategoryBrandRelationList(categoryBrandRelationList);
            return dto;
        }).collect(Collectors.toList());
        return new PageResult<>(total,collect);
    }
}
